package com.promote.hotspot.server.hot;

import com.promote.hotspot.server.tool.Constant;
import lombok.extern.java.Log;
import org.promote.hotspot.common.model.HotKeyModel;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author enping.jep
 * @date 2024/01/08 17:04
 **/
@Component
@Log
public class Consumer {

    @Resource
    private HotSpotQueue hotSpotQueue;

    @Resource
    private HotSpotHandler hotSpotHandler;


    @PostConstruct
    public void consume() {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < Constant.THREAD_COUNT; i++) {
            //创建一个线程池，如果线程池中的线程数量过大，它可以有效的回收多余的线程，如果线程数不足，那么它可以创建新的线程。
            executorService.submit(this::beginConsume);
        }
    }

    public void beginConsume() {
        while (true) {
            HotKeyModel model = hotSpotQueue.takeItemFromQueue();
            log.info("beginConsume==" + model.toString());
            hotSpotHandler.handle(model);//处理热点数据
            hotSpotQueue.addTotalDealCount();//处理完毕
        }
    }

}
